﻿using System;
using System.Reflection;
using Microsoft.Win32;

namespace Common.Tools
{
    /// <summary>
    /// 注册表
    /// </summary>
    public class RegistryHelper
    {
        private RegistryKey _Software;
        private RegistryKey _Company;
        private RegistryKey _Product;
        public RegistryHelper()
            : this(((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), true)).Company,
                ((AssemblyProductAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyProductAttribute), true)).Product)
        {

        }

        public RegistryHelper(string CompanyName, string ProductName)
        {
            _Software = Registry.LocalMachine.OpenSubKey("Software");
            _Company = _Software.OpenSubKey(CompanyName);
            if (null == _Company)
            {
                _Company = Registry.LocalMachine.CreateSubKey(@"Software\" + CompanyName);
            }
            _Product = _Company.OpenSubKey(ProductName, true);
            if (null == _Product)
            {
                _Product = Registry.LocalMachine.CreateSubKey(string.Format("Software\\{0}\\{1}", CompanyName, ProductName));
            }
        }

        /// <summary>
        /// 写注册表
        /// </summary>
        /// <param name="ValueName"></param>
        /// <param name="Value"></param>
        public void WriteRegistryValue(string ValueName, string Value)
        {
            _Product.SetValue(ValueName, Value);
        }

        /// <summary>
        /// 读注册表
        /// </summary>
        /// <param name="ValueName"></param>
        /// <returns></returns>
        public string ReadRegistryValue(string ValueName)
        {
            return (string)_Product.GetValue(ValueName);
        }
    }
}
